import { login, getUserInfo, updatePassword } from '@/api/user'

import { setToken, getToken, removeToken } from '@/utils/auth'

export default {
  namespaced: true,
  state() {
    return {
      token: getToken() || '',
      UserInfo: {}
    }
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    delToken(state) {
      state.token = ''
      removeToken()
    },
    setUserInfo(state, info) {
      state.UserInfo = info
    }

  },
  actions: {
    async login({ commit }, data) {
      const res = await login(data)
      commit('setToken', res.data)
    },
    async getUserInfo({ commit }) {
      const res = await getUserInfo()
      commit('setUserInfo', res.data)
    },
    logout({ commit }) {
      commit('delToken')
      commit('setUserInfo', {})
    },
    async updatePassword({ commit }, data) {
      await updatePassword(data)
    }
  }
}
